package com.gaojunxiang.controller;
import com.gaojunxiang.controller.result.Code;
import com.gaojunxiang.controller.result.Result;
import com.gaojunxiang.domain.User;
import com.gaojunxiang.service.UserServiceInter;
import com.gaojunxiang.service.impl.UserServiceImpl;
import com.gaojunxiang.system.exception.BusinessException;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired(required = false)
    private UserServiceInter userService;
  @GetMapping("/{uid}")
  public Result  getUserById(@PathVariable Integer uid){
     // return userService.findById(uid);
      User user = userService.findById(uid);
      //异常测试
  //    if(uid == 9){throw new BusinessException("查询失败！",Code.GET_ERROR);}
     return new Result(null != user ? Code.GET_OK : Code.GET_ERROR,user);
  }
  @GetMapping("/{page}/{size}")
    public Result getAll(@PathVariable Integer page,@PathVariable Integer size){
        //return userService.findAllUser(page,size);
      PageInfo<User> all = userService.findAllUser(page, size);
     return new Result(null != all ? Code.GET_OK : Code.GET_ERROR , all);
    }
    @DeleteMapping("/{uid}")
    public boolean  delete(@PathVariable Integer uid){
        return userService.delete(uid);
    }
    @PostMapping("/save")
    public Result  save( User user){
      //  return userService.save(user);
        boolean flag = userService.save(user);
      return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERROR);
    }
    @PutMapping("/update")
    public Result  update( User user){
       // return userService.update(user);
        boolean flag = userService.update(user);
       return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERROR);
    }

}
